using System.Collections.Generic;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;


namespace NodeCanvas.BehaviourTrees
{

    [Category("Composites")]
    [Description("Selects a child to execute based on its chance to be selected and returns Success if the child returns Success, otherwise picks another child.\nReturns Failure if all children return Failure, or a direct 'Failure Chance' is introduced.")]
    [ParadoxNotion.Design.Icon("ProbabilitySelector")]
    [Color("b3ff7f")]
    public class ProbabilitySelector_AutoChange : ProbabilitySelector
    {
        public List<(bool, float)> originChildWeights;
        public BBParameter<float> reduceProbability = 0.5f;

        protected override void OnSelectedNodeExecute(Status status, int index)
        {
            if (status == Status.Success)
            {
                if (index < originChildWeights.Count)
                {
                    var t = originChildWeights[index];
                    t.Item1 = true;
                    originChildWeights[index] = t;
                }
                if (index < childWeights.Count)
                {
                    childWeights[index] = childWeights[index].value * reduceProbability.value;
                }

                CheckResetWeights();
            }
        }

        protected override void OnReset()
        {
            base.OnReset();
            if (childWeights != null)
            {
                if (originChildWeights == null || originChildWeights.Count != childWeights.Count)
                {
                    originChildWeights = new List<(bool, float)>(childWeights.Count);
                    for (int i = 0; i < childWeights.Count; i++)
                    {
                        originChildWeights.Add((false, childWeights[i].value));
                    }
                }
            }
        }

        void CheckResetWeights()
        {
            if (originChildWeights != null)
            {
                for (int i = 0; i < originChildWeights.Count; i++)
                {
                    if (!originChildWeights[i].Item1)
                    {
                        return;
                    }
                }

                if (originChildWeights.Count == childWeights.Count)
                {
                    for (int i = 0; i < originChildWeights.Count; i++)
                    {
                        var t = originChildWeights[i];
                        t.Item1 = false;
                        childWeights[i] = t.Item2;
                        originChildWeights[i] = t;
                    }
                }
            }
        }

#if UNITY_EDITOR
        protected override void OnNodeInspectorGUI()
        {
            base.OnNodeInspectorGUI();
            if (outConnections.Count == 0)
            {
                return;
            }

            GUILayout.Space(5);
            GUILayout.BeginHorizontal();
            reduceProbability = (BBParameter<float>)NodeCanvas.Editor.BBParameterEditor.ParameterField("Reduce Weight Precent", reduceProbability);
            GUILayout.EndHorizontal();
        }
#endif
    }
}